<?php

/**
 * 默认控制器
 * @author scabish
 * @since 2015-11-26 16:27:04
 */
class IndexController extends SCController {

    private $params = [];
    
    public function __construct() {
        parent::__construct();
    }
    
    public function Index() {
        SCS::request()->isPost() || SCS::request()->route('doc');
        Abyss::Instance()->Run();
    }
    
    public function doc() {
        $files = scandir(SC_CONTROLLER_PATH);
        sort($files);
        unset($files[0], $files[1]);
        foreach($files as $controller) {
            $controller = substr($controller, 0, strpos($controller, '.'));
            if(in_array($controller, ['IndexController', 'DocController'])) continue;
            $rc = new ReflectionClass($controller);
            $methods = $rc->getMethods(ReflectionMethod::IS_PUBLIC);
            $class = $rc->newInstance();
            foreach($methods as $i => $method) {
                if(preg_match('/^_+/', $method->name)) unset($methods[$i]);
                $action = $rc->getMethod($method->name);
                $action->invoke($class, null);
            }
        }
        
        $doc = Abyss::Instance()->GetDoc();
        
        #echo '<pre>';print_r($doc);die;
        $this->view->render(get_defined_vars());
    }
    
    public function Test() {
        $query = [[
            'api' => 'User/Create',
            'param' => ['username' => 'keluo', 'password' => 'xxx'],
            'bind' => ['::userId' => '.userId'], // 调用下一个api需要用到的参数：userId
            'return' => '.userId'
        ], [
            'api' => 'User/Status',
            'param' => ['userId' => '::userId']
        ]];
        
        list($userId, $status) = Abyss::Instance()->Sink($query);
        
        echo $userId, '<br/>', $status;
    }
}